package net.lanelife.casino.caipiao.job.handler;

import java.util.List;
import java.util.Map;

import net.lanelife.casino.caipiao.entity.BankTransactionDetail;
import net.lanelife.casino.caipiao.entity.TransactionDetail;
import net.lanelife.casino.caipiao.entity.User;
import net.lanelife.casino.caipiao.service.api.IBankTransactionDetailService;
import net.lanelife.casino.caipiao.service.api.ICompanyDailyStatisticsService;
import net.lanelife.casino.caipiao.service.api.ICompanyStatisticsService;
import net.lanelife.casino.caipiao.service.api.ITeamDailyStatisticsService;
import net.lanelife.casino.caipiao.service.api.ITeamStatisticsService;
import net.lanelife.casino.caipiao.service.api.ITransactionDetailService;
import net.lanelife.casino.caipiao.service.api.IUserDailyStatisticsService;
import net.lanelife.casino.caipiao.service.api.IUserService;
import net.lanelife.casino.caipiao.service.api.IUserStatisticsService;
import net.lanelife.casino.common.utils.StringUtils;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.springframework.beans.factory.annotation.Autowired;

public class TransactionJobHandler {
	
	@Autowired
	protected IUserStatisticsService userStatisticsService;
	
	@Autowired
	protected IUserDailyStatisticsService userDailyStatisticsService;
	
	@Autowired
	protected ITeamStatisticsService teamStatisticsService;
	
	@Autowired
	protected ITeamDailyStatisticsService teamDailyStatisticsService;
	
	@Autowired
	protected ICompanyStatisticsService companyStatisticsService;
	
	@Autowired
	protected ICompanyDailyStatisticsService companyDailyStatisticsService;
	
	@Autowired
	protected IUserService userService;
	
	@Autowired
	protected ITransactionDetailService transactionDetailService;
	
	@Autowired
	protected IBankTransactionDetailService bankTransactionDetailService;
	
	
	/**
	 * 保存交易明细
	 * @param billNo  单号
	 * @param dir     方向
	 * @param type    类型
	 * @param time    时间
	 * @param amount  金额
	 * @param pb      交易前余额
	 * @param cb      交易后余额
	 * @throws Exception 
	 */
	protected void saveTransactionDetail(User user, String billNo, TransactionDetail.TransactionDirection dir, TransactionDetail.TransactionType type, DateTime time, Double amount, Double pb, Double cb) throws Exception {
		TransactionDetail transactionDetail = new TransactionDetail(user);
		transactionDetail.setBillNo(billNo);
		transactionDetail.setTransactionDirection(dir);
		transactionDetail.setTransactionType(type);
		transactionDetail.setTransactionTime(time);
		transactionDetail.setTransactionAmount(amount);
		transactionDetail.setPreviousBalance(pb);
		transactionDetail.setCurrentBalance(cb);
		transactionDetailService.save(transactionDetail);
	}
	
	/**
	 * 保存银行交易明细
	 * @param dir      方向
	 * @param sut      源用途
	 * @param time     交易时间
	 * @param sbn      源银行
	 * @param san      源户名
	 * @param sano     源卡号
	 * @param dbn      目标银行
	 * @param dan      目标户名
	 * @param dano     目标卡号
	 * @param amount   交易金额
	 * @param fee      手续费
	 * @param cb       交易后余额
	 * @param userName 用户帐号
	 * @throws Exception
	 */
	protected void saveBankTransactionDetail(BankTransactionDetail.TransactionDirection dir, BankTransactionDetail.SourceUsesType sut, DateTime time, String sbn, String san, String sano, String dbn, String dan, String dano, Double amount, Double fee, Double cb, String userName) throws Exception {
		BankTransactionDetail bankTransactionDetail = new BankTransactionDetail();
		bankTransactionDetail.setTransactionDirection(dir);
		bankTransactionDetail.setTransactionTime(time);
		bankTransactionDetail.setSourceUsesType(sut);
		bankTransactionDetail.setSourceBankName(sbn);
		bankTransactionDetail.setSourceAccountName(san);
		bankTransactionDetail.setSourceAccountNo(sano);
		bankTransactionDetail.setDestinationBankName(dbn);
		bankTransactionDetail.setDestinationAccountName(dan);
		bankTransactionDetail.setDestinationAccountNo(dano);
		bankTransactionDetail.setTransactionAmount(amount);
		bankTransactionDetail.setTransactionFee(fee);
		bankTransactionDetail.setCurrentBalance(cb);
		bankTransactionDetail.setUserName(userName);
		bankTransactionDetailService.save(bankTransactionDetail);
	}

	/**
	 * 更新交易报表：
	 *    用户统计表
	 *    用户日结表
	 *    团队统计表
	 *    团队日结表
	 *    公司统计表
	 *    公司日结表
	 * @param user
	 * @param reportDate
	 * @param properties
	 * @throws Exception
	 */
	protected void updateTransactionStatistics(User user, LocalDate reportDate, Map<String, Object> properties) throws Exception {
		// 更新用户统计表
		updateUserStatistics(user, properties);
		// 更新用户日结表
		updateUserDailyStatistics(user, reportDate, properties);
		List<User> teamUsers = userService.findAll(StringUtils.toIntegerList(user.getPath(), "\\|"));
		for (User tuser : teamUsers) {
			// 更新团队统计表
			updateTeamStatistics(tuser, properties);
			// 更新团队日结表
			updateTeamDailyStatistics(tuser, reportDate, properties);
		}
		// 更新公司统计表
		updateCompanyStatistics(properties);
		// 更新公司日结表
		updateCompanyDailyStatistics(reportDate, properties);
	}
	
	/**
	 * 更新用户统计表
	 * @param user
	 * @param properties
	 * @return
	 * @throws Exception 
	 */
	protected int updateUserStatistics(User user, Map<String, Object> properties) throws Exception {
		return userStatisticsService.update(user, properties);
	}
	
	/**
	 * 更新用户日结表
	 * @param user
	 * @param reportDate
	 * @param properties
	 * @return
	 * @throws Exception 
	 */
	protected int updateUserDailyStatistics(User user, LocalDate reportDate, Map<String, Object> properties) throws Exception {
		return userDailyStatisticsService.update(user, reportDate, properties);
	}
	
	/**
	 * 更新团队统计表
	 * @param user
	 * @param properties
	 * @return
	 * @throws Exception 
	 */
	protected int updateTeamStatistics(User user, Map<String, Object> properties) throws Exception {
		return teamStatisticsService.update(user, properties);
	}
	
	/**
	 * 更新团队日结表
	 * @param user
	 * @param reportDate
	 * @param properties
	 * @return
	 * @throws Exception 
	 */
	protected int updateTeamDailyStatistics(User user, LocalDate reportDate, Map<String, Object> properties) throws Exception {
		return teamDailyStatisticsService.update(user, reportDate, properties);
	}
	
	/**
	 * 更新公司统计表
	 * @param properties
	 * @return
	 * @throws Exception 
	 */
	protected int updateCompanyStatistics(Map<String, Object> properties) throws Exception {
		return companyStatisticsService.update(properties);
	}
	
	/**
	 * 更新公司日结表
	 * @param reportDate
	 * @param properties
	 * @return
	 * @throws Exception 
	 */
	protected int updateCompanyDailyStatistics(LocalDate reportDate, Map<String, Object> properties) throws Exception {
		return companyDailyStatisticsService.update(reportDate, properties);
	}
	
}
